<?
if(isset($_GET['method']))
{
	$method = $_GET['method'];
	if($method=="del"){
		$sListId="";
		if(isset($_POST["chon"]))$sListId=$_POST["chon"];
		$sql="delete from ".$prefix."tukhoa where tk_id in('".str_replace(",","','",$sListId)."')";
		mysql_query($sql);
		Redirect("Bạn đã xoá thành công","?mod=tukhoa");	
	}else if($method=="delete"){
		$IdList=$_GET["id"];
		$sql="delete from ".$prefix."tukhoa where tk_id=$IdList";
		mysql_query($sql);
		Redirect("Đã xóa thành công","?mod=tukhoa");		
	}
}
	$strSQL = "select * from ".$prefix."tukhoa";
	
	///////Paging
	$pp=10; 
	$parr=array();
	$parr=paging($strSQL,$pp);
	$page=$parr[0];
	$limitvalue=$parr[1];
	$numofpages=$parr[2];
	$strSQL .= " order by tk_id DESC";
	$strSQL.=" LIMIT $limitvalue, $pp";
	$rs = $db->query($strSQL);
	$i=0;
	$isexits=false;
	
	if(mysql_num_rows($rs)) $isexits=true;
?>
<div class="newsList-path"></div>
	<div style="margin-bottom:5px; font-size:14px;">
	 Có <?=$db->num_rows($rs)?>
	  từ khóa được tìm thấy </div>
<div align="right" class="newsList-search-up">
	<input type="button" name="them" value="Thêm Từ Khóa" onclick="javascript: window.location='?mod=tukhoa&act=add';" />
</div>
<form name="frmListSp" method="post" action="?mod=tukhoa&method=del" onSubmit="return checkInput(this,'Bạn hãy chọn ít nhất một giá trị','Bạn chắc chắn muốn xoá không');">
	<input type="hidden" name="chon" />
	<table width="100%" border="1" bordercolor="#B2DAEE" cellpadding="4">
		<tr class="newsList-content-head">
		<td width="4%" class="newsList-content-view" align="center"><input type="checkbox" name="chkall" value="0" <? if($isexits) echo "onClick=\"docheck(this.form,0);\""?>></td>
			<td width="47%" class="newsList-content-title">Từ khóa</td>
			<td width="31%" class="newsList-content-title">Chủng loại </td>
	      <td width="18%" class="newsList-content-view"><div align="center">Tùy chọn </div></td>
		</tr>
				<?
				while($rows = $db->fetch_array($rs))
				{
					$i++;
					$tk_id = $rows['tk_id'];
					$tk_tukhoa = $rows['tk_tukhoa'];
					$tk_loaisanpham = $rows['tk_loaisanpham'];
				?>				
		<tr onmouseover="this.bgColor='#FEF2CB';" onmouseout="this.bgColor='#FFFFFF';" class="newsList-content-list" <? if($i%2==0) echo "bgcolor='#f2fff2'";?>>
			<td class="newsList-content-title" align="center"><input type="checkbox" name="Xoa" value="<?=$tk_id?>" onClick="docheckone(this.form);"></td>
			<td class="newsList-content-title"><?=$tk_tukhoa?></td>
			<td class="newsList-content-title">
            	<table cellpadding="0" cellspacing="0" width="100%">
                <tr>
					<td>
						
						<?=loadTenLoaiSanPham($tk_loaisanpham)?>				</td>
              </table>			</td>
	      <td class="newsList-content-view"><div align="center">
		 <a href="?mod=tukhoa&act=edit&id=<?=$tk_id?>" class="menu"><img src="images/edit.gif" border="0" /></a>&nbsp;
		  <a href="?mod=tukhoa&amp;method=delete&amp;id=<?=$tk_id?>" class="menu" onclick="return confirm('Bạn có chắc chắn muốn xóa không?')"><img src="images/delete_on.gif" border="0" /></a></div></td>
        </tr>
		<? }?>
	<tr>
   <td colspan="13" align="center">
   <input type="submit" name="submit" value="   Xoá   " class="Button">
   <input type="reset" value="Làm lại" class="Button">   </td>
   </tr>
</table>
</form>
	<div class="newsList-pagging">
	<div class="raovat-paging-bound">
	<span style="padding:5px;">
	<p align="center" class="font">
			<?php echo page_div( "?mod=tukhoa&p=%d_pg", "10", ceil ( $numofpages ), $page ); ?>
	</p>
	</span>
<?
	$act = $_GET['act'];
	
	if(isset($act) && ( ($act == 'add') || ($act == 'edit') ) ){
	
	if($act == 'edit'){
		if(isset($_GET['id'])){
			$id = intval($_GET['id']);
			$rs = $db->query("SELECT * FROM ".$prefix."tukhoa WHERE tk_id = '".$id."'");
			if($db->num_rows($rs) != 0){
				$row = $db->fetch_array($rs);
				$tukhoa = $row[tk_tukhoa];
				$loaisanpham = $row[tk_loaisanpham];
			}else{
				header("Location: ?");
			}
			
			$submit = "Sửa";
			$action = "?mod=tukhoa&act=edit&id=".$id."";
			///////////
			if(isset($_POST['submit'])){
				$chungloai = $_POST['chungloai'];
				$tukhoa = $_POST['tukhoa'];
				$db->query("UPDATE ".$prefix."tukhoa SET tk_loaisanpham = '".$chungloai."', tk_tukhoa = '".$tukhoa."' WHERE tk_id = '".$id."'");
				header("Location: ?mod=tukhoa");
			}
		}else{header("Location: ?mod=tukhoa");}
		
		///////////
	}else{
		$submit = "Thêm"; 
		$action = "?mod=tukhoa&act=add";
		if(isset($_POST['submit'])){
			$chungloai = $_POST['chungloai'];
			$tukhoa = $_POST['tukhoa'];	
			
			$db->query("INSERT INTO ".$prefix."tukhoa(tk_loaisanpham,tk_tukhoa) VALUES('".$chungloai."','".tukhoa."')");
			header("Location: ?mod=tukhoa&act=add");
		}
	}
	
?>
	<br />
	<div align="center" style="border:1px solid #FF9900; padding:10px;">
	  <form id="form1" name="form1" method="post" action="<?=$action?>">
	    <table cellpadding="5" cellspacing="0" border="0">
          <tr>
            <td width="139">Tên chủng loại </td>
            <td width="266">
			<select name="chungloai" id="chungloai">
				<?
					$rs = $db->query("SELECT * FROM ".$prefix."loaisanpham WHERE c_hienthi = 1 AND c_id_cha = 0");
					while($row = $db->fetch_array($rs)){
					$c_ten = $myString->unQuote(stripslashes($row[c_ten]));
					$c_id = $row[c_id];
				?>	
                    <optgroup label="<?=$c_ten?>">
					<?
						$rs2 = $db->query("SELECT * FROM ".$prefix."loaisanpham WHERE c_hienthi = 1 AND c_id_cha = '".$c_id."'");
						while($row2 = $db->fetch_array($rs2)){
						$c_id2 = $row2[c_id];
						$c_ten2 = $row2[c_ten];
					?>
						<option value="<?=$c_id2?>" <? if($loaisanpham == $c_id2){?> selected="selected" <? } ?>><?=$c_ten2?></option>
					<? } ?>
					</optgroup>
				<? } ?>
            </select>            </td>
          </tr>
          <tr>
            <td>Tên từ khóa </td>
            <td><input name="tukhoa" type="text" id="tukhoa" value="<?=$tukhoa?>" size="30"/></td>
          </tr>
          <tr>
		  	<td></td>
            <td><input name="submit" type="submit" id="submit" value="<?=$submit?>" /></td>
          </tr>
        </table>
      </form>
	  </div>
<? } ?>
	</div>
	</div>
	<div class="newsList-button-back"><a href="javascript: history.back()">[Quay lại]</a></div>